package com.wut.jmx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wut.jmx.common.R;
import com.wut.jmx.entity.Employee;
import com.wut.jmx.service.EmployeeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@RestController
@Slf4j
@Api(tags = "员工管理")
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@ApiParam("登录对象") @RequestBody Employee employee) {
        log.info("员工登录,登录名:" + employee.getUsername());
        //构建查询对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes(StandardCharsets.UTF_8));
        queryWrapper.eq(Employee::getPassword, password);
        Employee employee1 = employeeService.getOne(queryWrapper);
        if (employee1 == null) {
            log.error("输入的用户名或密码错误");
            return R.error("输入的用户名或密码无效");
        }
        if (employee1.getStatus()==0){
            return R.error("该账号已被禁用!");
        }
        log.info("打印id:"+employee1.getId());
        request.getSession().setAttribute("employeeId",employee1.getId());
        return R.success(employee1);
    }
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        log.info("员工退出系统,员工id:"+request.getSession().getAttribute("employeeId"));
        request.getSession().removeAttribute("request.getSession()");
        return R.success("退出成功");
    }
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        //构建分页对象
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)){
            queryWrapper.like(Employee::getName,name);
        }
//        queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        Page result = employeeService.page(pageInfo, queryWrapper);
        return R.success(result);
    }
    @ApiOperation("通过id查询员工")
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable String id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
    @ApiOperation("通过id更新员工信息")
    @PutMapping
        public R<Employee> updateById(HttpServletRequest request,@RequestBody Employee updateEmployee) {
        log.info("update employee:{}",updateEmployee);
//        String id = updateEmployee.getId();
//        Employee employee = employeeService.getById(id);
//        employee.setUsername(updateEmployee.getUsername());
//        employee.setName(updateEmployee.getName());
//        employee.setPhone(updateEmployee.getPhone());
//        employee.setSex(updateEmployee.getSex());
//        employee.setIdNumber(updateEmployee.getIdNumber());
        log.info("打印更新:{}",request.getSession().getAttribute("employeeId"));
//        Integer updateUserId = Integer.valueOf(request.getSession().getAttribute("employeeId").toString());
//        updateEmployee.setUpdateUser(updateUserId);
//        updateEmployee.setUpdateTime(LocalDateTime.now());
        boolean update = employeeService.updateById(updateEmployee);
        if (update){
            return R.success(updateEmployee);
        }
        else {
            return R.error("失败");
        }
    }
    @ApiOperation("保存员工信息")
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee saveEmployee){
        log.info("新增员工,员工信息:{}",saveEmployee.toString());
        saveEmployee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
//        saveEmployee.setCreateTime(LocalDateTime.now());
//        Integer updateUserId = Integer.valueOf(request.getSession().getAttribute("employeeId").toString());
//        saveEmployee.setCreateUser(updateUserId);
//        saveEmployee.setUpdateTime(LocalDateTime.now());
//        saveEmployee.setUpdateUser(updateUserId);
        employeeService.save(saveEmployee);
        return R.success("新增员工成功");
    }
}
